<?php
require_once(FWK_MODEL."AbsComponente.class.php");


class Grid extends AbsComponente{

	private $objDaoComp;
	private $strMetodoDao;
	private $pagAtual;
	private $limit;

	private $param1;
	private $param2;
	private $param3;
	private $param4;
	private $isFilter;
	private $arrCampos;

	private $mantemParams = true;

	public function setDaoComp($daoComp){
		require_once(PASTA_DAO.$daoComp.".class.php");

		$this->objDaoComp = new $daoComp();
	}

	public function getDaoComp(){
		return $this->objDaoComp;
	}

	public function setMetodoDao($strMetodoDao){
		$this->strMetodoDao = $strMetodoDao;
	}

	public function getMetodoDao(){
		return $this->strMetodoDao;
	}

	public function setPagAtual($pag){
		$this->pagAtual = $pag;
	}

	public function getPagAtual(){
		return $this->pagAtual;
	}

	public function setLimit($limit){
		$this->limit = $limit;
	}

	public function getLimit(){
		return (int)$this->limit;
	}


	public function getComponente($value = null){

		$pagAtual = self::getPagAtual();
		if($pagAtual == "" || !isset($pagAtual) || !is_numeric($pagAtual)){
			$pagAtual = 0;
		}else{
			$pagAtual = (int)$pagAtual - 1;
		}
		
		$tipo_campo = isset($_GET["tipo_campo"])?$_GET["tipo_campo"]:"";
    	$valor_param = isset($_GET["valor_param"])?$_GET["valor_param"]:"";
		
		
		
		$pagAtual = $pagAtual < 0 ? 0 : $pagAtual;
		$numPags = self::getNumPaginas();
		$proxPag = ($pagAtual+2) > $numPags ? $numPags : ($pagAtual+2);

		$arrDados = self::getDaoComp()->getListaGrid(self::getLimit() * $pagAtual, self::getLimit(), false, $tipo_campo, $valor_param);
		$arrDados = Utf8Parsers::matrizUtf8Encode($arrDados);

		parent::getObjSmarty()->assign("filtro",self::isFilter());
		if(self::isFilter()){
			if(count(self::getCamposFiltro()) > 0){
				parent::getObjSmarty()->assign("arrOptions",self::getCamposFiltro());
			}
		}
		
		parent::getObjSmarty()->assign("arrColunas",self::getNomeColunas($arrDados));
		if(count($arrDados) > 0)
		parent::getObjSmarty()->assign("pagAtual",($pagAtual + 1));

		parent::getObjSmarty()->assign("pagAnterior",$pagAtual);
		parent::getObjSmarty()->assign("proxPag",$proxPag);

		parent::getObjSmarty()->assign("numPags",$numPags);
		parent::getObjSmarty()->assign("numCols",self::getNumColunas($arrDados));
		parent::getObjSmarty()->assign("arrDados",$arrDados);
		parent::getObjSmarty()->assign("numRegs",self::getNumRegistros());

		if(self::getMantemParams()){
			$strParams = "&";
			foreach ($_GET as $key => $value) {
				if($key !="" && $value != "" && $key != "p")
					$strParams.= $key."=".$value."&";
			}
			$strParams = substr($strParams, 0, -1);
			parent::getObjSmarty()->assign("paramsPag",$strParams);
			
//			print("<pre>");
//			print_r($_GET);
//			print($strParams);
//			die();
			
		}

		$strTela = parent::getObjSmarty()->fetch("../".TPLS_FWK."grid.tpl");

		print($strTela);
	}

	private function getNomeColunas($arrDados){
		$arrCols = array();
		if(count($arrDados) > 0){
			foreach ($arrDados[0] as $value => $campo) {
				if(!is_numeric($value))
				$arrCols[] = $value;
			}
		}
		return array_slice($arrCols, 1);
	}

	private function getNumColunas($arrDados){
		$numCols = 0;
		if(count($arrDados) > 0){
			foreach ($arrDados[0] as $value => $campo) {
				if(!is_numeric($value))
				$numCols++;
			}
		}
		return $numCols;
	}

	private function getNumRegistros(){
		$tipo_campo = isset($_GET["tipo_campo"])?$_GET["tipo_campo"]:"";
    	$valor_param = isset($_GET["valor_param"])?$_GET["valor_param"]:"";
		$arrDados = self::getDaoComp()->getListaGrid(0, 0, true, $tipo_campo, $valor_param);
		if(isset($arrDados[0][0]))
			$numRegs = $arrDados[0][0];
		else 
			$numRegs = 0;
		return $numRegs;
	}

	private function getNumPaginas(){
		$numRegs = self::getNumRegistros();
		$numPorPags = self::getLimit();
		return ceil($numRegs / $numPorPags);

	}

	public function setParam1($param1){
		$this->param1 = $param1;
		self::getDaoComp()->setParam1($param1);
	}

	public function getParam1(){
		return $this->param1;
	}

	public function setParam2($param2){
		$this->param2 = $param2;
		self::getDaoComp()->setParam2($param2);
	}

	public function getParam2(){
		return $this->param2;
	}

	public function setParam3($param3){
		$this->param3 = $param3;
		self::getDaoComp()->setParam3($param3);
	}

	public function getParam3(){
		return $this->param3;
	}

	public function setParam4($param4){
		$this->param4 = $param4;
		self::getDaoComp()->setParam4($param4);
	}

	public function getParam4(){
		return $this->param4;
	}

	public function getMantemParams(){
		return $this->mantemParams;
	}

	public function setMantemParams($mantemParams){
		$this->mantemParams = $mantemParams;
	}

	public function isFilter(){
		return $this->isFilter;
	}

	public function setFilter($filtro){
		$this->isFilter = $filtro;
	}

	public function setCamposFiltro($arrCampos){
		$this->arrCampos = $arrCampos;
	}

	public function getCamposFiltro(){
		return $this->arrCampos;
	}


}